<template>
  <div class="user-content">
    <!-- 顶部按钮 -->
    <div class="header">
      <el-button type="primary" @click="addInfo">
        <i class="el-icon-plus"></i>
        <span>新增</span>
      </el-button>
      <div class="search">
        <el-input
          placeholder="请输入内容"
          v-model="seachText"
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="searchData"
            @keyup.enter="searchData"
          ></el-button>
        </el-input>
      </div>
    </div>
    <!-- 列表数据 -->
    <div class="table-content">
      <el-table :data="userTableData" style="width: 100%" height="90%">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="sex" label="性别" width="180">
          <template slot-scope="scope">
            {{ scope.row.sex === 0 ? "男" : "女" }}
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄"> </el-table-column>
        <el-table-column prop="birth" label="出生日期"> </el-table-column>
        <el-table-column prop="address" label="地址"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部分页 -->
    <div class="footer-content">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="pageChange"
      >
      </el-pagination>
    </div>
    <!-- 对话框组件 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <!-- form表单 -->
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        :inline="true"
        label-position="right"
        label-width="80px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option label="男" :value="0"></el-option>
                <el-option label="女" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="日期" prop="birth">
              <el-date-picker
                v-model="form.birth"
                type="date"
                placeholder="请选择日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input
                v-model="form.address"
                placeholder="请输入地址"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/*
 * @desc 用户个人中心
 * @author rz
 * @date 2024-2-20
 */
import { getUser, addUser, editUser, delUser } from "../api";
export default {
  data() {
    return {
      addOrEditFlag: "add",
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: null,
        address: "",
      },
      // 模糊搜索内容
      seachText: "",
      // 用户列表数据
      userTableData: [],
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        birth: [{ required: true, message: "请输入日期", trigger: "blur" }],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }],
      },
      // 分页总数
      total: 0,
      // 分页数据
      pageInfo: {
        page: 1,
        limit: 10,
      },
    };
  },
  methods: {
    // 新增数据
    addInfo() {
      this.addOrEditFlag = "add";
      this.dialogVisible = true;
    },
    // 对话框关闭时的操作
    handleClose() {
      this.$refs.form.resetFields();
      this.form = {};
      this.dialogVisible = false;
    },
    // 确定表单
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // 对新增或编辑进行判断处理
          if (this.addOrEditFlag === "add") {
            addUser(this.form).then((res) => {
              // 重新获取列表数据
              this.handleGetData();
            });
          } else if (this.addOrEditFlag === "edit") {
            editUser(this.form).then((res) => {
              // 重新获取列表数据
              this.handleGetData();
            });
          }
          this.handleClose();
        }
      });
    },
    // 取消表单
    cancel() {
      this.handleClose();
    },
    // 查询数据
    handleGetData() {
      getUser({ params: { name: this.seachText, ...this.pageInfo } }).then(
        (res) => {
          this.userTableData = res.data.list;
          this.total = res.data.count || 0;
        }
      );
    },
    // 编辑数据
    handleEdit(index, row) {
      this.addOrEditFlag = "edit";
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    // 删除数据
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser(row).then((res) => {
            if (res.status === 200) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              // 重新获取列表数据
              this.handleGetData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 模糊搜索
    searchData() {
      this.handleGetData();
    },
    // 切换页码
    pageChange(val) {
      this.pageInfo.page = val;
      this.handleGetData();
    },
  },
  computed: {
    // 对话框标题
    title() {
      return this.addOrEditFlag === "add" ? "新增" : "编辑";
    },
  },
  mounted() {
    this.handleGetData();
  },
};
</script>

<style lang="less" scoped>
.el-input {
  width: 220px;
}
.user-content {
  height: 90%;
  position: relative;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .table-content {
    height: 100%;
  }
  .footer-content {
    position: absolute;
    right: 20px;
    bottom: 50;
  }
}
</style>